﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
///////////////////////////////////////////////////////////////////////////
// 文件名:     serverfiledialog.cpp
// 描述:
// 作者:      颜磊
// 创建时间:   2024-06-09 00:48
///////////////////////////////////////////////////////////////////////////
#include "serverfiledialog.h"
#include "ui_serverfiledialog.h"
#include <QDebug>
#include <QMessageBox>
#include <QPushButton>
#include <QFileIconProvider>
#include <qstringtools.h>

ServerFileDialog::ServerFileDialog(uint64_t m_connectId,QString &filePath,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ServerFileDialog)
{
    ui->setupUi(this);
    ui->buttonBox->button(QDialogButtonBox::Ok)->setText(QString("确定"));
    ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(QString("取消"));

    connectId = m_connectId;
    pFilePath = &filePath;
    if(!filePath.isEmpty()){
        QString dirPath = filePath.left(filePath.lastIndexOf("/"));
        QString fileName = filePath.right(filePath.length() - filePath.lastIndexOf("/")-1);
        ui->floderPath->setText(dirPath);
        ui->fileName->setText(fileName);
        serverFileHeader.setFolder(dirPath.toStdString());
    }
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!ui->fileName->text().isEmpty());
    tableModel = new QStandardItemModel();
    tableModel->setHorizontalHeaderItem(0,new QStandardItem(QString("名称")));
    tableModel->setHorizontalHeaderItem(1,new QStandardItem(QString("类型")));
    tableModel->setHorizontalHeaderItem(2,new QStandardItem(QString("大小")));
    ui->tableView->setModel(tableModel);

    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//单行选中
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置单元格不可编辑
    ui->tableView->verticalHeader()->hide();
    //ui->tableView->horizontalHeader()->setMinimumSectionSize(100);//最小宽度
    ui->tableView->setColumnWidth(0,300);
}

ServerFileDialog::~ServerFileDialog()
{
    delete tableModel;
    delete ui;
}

void ServerFileDialog::getFileList()
{
    qDebug() << __FUNCTION__;
    tableModel->removeRows(0,tableModel->rowCount());
    serverFileHeader.setFolder(ui->floderPath->text().toStdString());
    serverFileHeader.setLikeName(ui->likeName->text().toStdString());
    QStringList filters = {"*.*","*.exe"};
    serverFileHeader.setFilter(filters.at(ui->filter->currentIndex()).toStdString());
    signal_getServerFileList(connectId,serverFileHeader);
}

void ServerFileDialog::slot_serverFileList(uint64_t connectId, ServerFileHeader m_serverFileHeader, QList<ServerFile> serverFileList)
{
    serverFileHeader = m_serverFileHeader;
    QFileIconProvider iconProvider;
    QIcon icon;
    QStringTools stringTools;
    for (int i=0;i<serverFileList.size();i++) {
        tableModel->insertRow(tableModel->rowCount());
        int inserRow = tableModel->rowCount() -1;
        if(serverFileList.at(i).isFolder){
            icon = iconProvider.icon(QFileIconProvider::Folder);
        }else{
            icon = iconProvider.icon(QFileIconProvider::File);
        }
        tableModel->setItem(inserRow,0,new QStandardItem(icon,QString(serverFileList.at(i).fileName)));
        tableModel->setItem(inserRow,1,new QStandardItem(serverFileList.at(i).isFolder ? QString("文件夹") : QString("文件")));
        tableModel->setItem(inserRow,2,new QStandardItem(serverFileList.at(i).isFolder ? QString("") : stringTools.byteSizeToString(serverFileList.at(i).fileSize)));
    }
    //    ui->tableView->setColumnHidden(0,true);
}

void ServerFileDialog::on_getBtn_clicked()
{
    getFileList();
}

void ServerFileDialog::on_tableView_clicked(const QModelIndex &index)
{
    int row = index.row();
    bool isFloder = (tableModel->index(row,1).data().toString() == QString("文件夹"));
    QString clickName = tableModel->index(row,0).data().toString();
    if(isFloder){
        ui->fileName->setText("");
    }else{
        ui->fileName->setText(clickName);
    }
}

void ServerFileDialog::on_tableView_doubleClicked(const QModelIndex &index)
{
    int row = index.row();
    bool isFloder = (tableModel->index(row,1).data().toString() == QString("文件夹"));
    QString clickName = tableModel->index(row,0).data().toString();
    if(isFloder){
        if(ui->floderPath->text().isEmpty()){
            ui->floderPath->setText(clickName);
        }else{
            ui->floderPath->setText(ui->floderPath->text() + "/" +clickName);
        }
        getFileList();
    }else{
        ui->fileName->setText(clickName);
        accept();
    }
}

void ServerFileDialog::on_homeBtn_clicked()
{
    ui->floderPath->setText("");
    getFileList();
}

void ServerFileDialog::on_lastPath_clicked()
{
    QString dirPath = ui->floderPath->text();
    if(dirPath.lastIndexOf("/") == -1){
        dirPath = "";
    }else{
        dirPath = dirPath.mid(0,dirPath.lastIndexOf("/"));
    }
    ui->floderPath->setText(dirPath);
    getFileList();
}

void ServerFileDialog::accept()
{
    if(!ui->fileName->text().isEmpty()){
        *pFilePath = ui->floderPath->text() + "/" + ui->fileName->text();
        QDialog::accept();
    }else{
        QMessageBox::information(this,QString("提示："),QString("请选择文件"));
    }
}

void ServerFileDialog::on_filter_currentIndexChanged(int index)
{
    getFileList();
}

void ServerFileDialog::on_fileName_textChanged(const QString &arg1)
{
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!arg1.isEmpty());
}
